tongfusoft.group = function() {
}

tongfusoft.group.prototype = {
		//安全组列表
		groupTable : function() {

			var table = new tongfusoft.table();
			var columns = [
					{
						"data" : "groupId"
					},
					{
						"data" : "groupName"
					},
					{
						"data" : "loginName"
					},
					{
						"data" : "lastUpdDate"
					},
					{
						"data" : "groupId",
						"render" : function(data, type, full, meta) {
							var delbut = '<button groupId="'
									+ data
									+ '" onclick="new tongfusoft.group().groupDel(this)" type="button" data-toggle="modal" data-target="#delgroup" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
							var editbut = '<button groupId="'
									+ data
									+ '" onclick="new tongfusoft.group().groupEdit(this)" type="button" data-toggle="modal" data-target="#delgroupClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

							return editbut + delbut;
						}

					} ]

			table.dataTable('groupTable', 'manager/group/query', columns, null);

			$('#groupTable').DataTable().on(
					'select',
					function(e, dt, type, indexes) {
						$("#groupId").val($('#groupTable').DataTable().rows(indexes).data()[0].groupId);
						$("#groupTableTitle").text(
								"安全组名："+$('#groupTable').DataTable().rows(indexes).data()[0].groupName);
						$('#powerTable').DataTable().clear();
						$('#powerTable').DataTable().ajax.url(
								common.getContextPath('manager/power/query?groupId=' + $("#groupId").val())).load();
					});

		},
		
		//权限列表
		powerTable : function() {

			var table = new tongfusoft.table();
			var columns = [
					{
						"data" : "rpId"
					},
					{
						"data" : "roleName"
					},
					{
						"data" : "readPower",
						"render" : function(data, type, full, meta) {
							if(data=='1'){
								return '<em class="fa fa-check"></em>'
							}else{
								return ''
							}
						}
					},
					{
						"data" : "updatePower",
						"render" : function(data, type, full, meta) {
							if(data=='1'){
								return '<em class="fa fa-check"></em>'
							}else{
								return ''
							}
						}
					},
					{
						"data" : "deletePower",
						"render" : function(data, type, full, meta) {
							if(data=='1'){
								return '<em class="fa fa-check"></em>'
							}else{
								return ''
							}
						}
					},
					{
						"data" : "managerPower",
						"render" : function(data, type, full, meta) {
							if(data=='1'){
								return '<em class="fa fa-check"></em>'
							}else{
								return ''
							}
						}
					},
					{
						"data" : "rpId",
						"render" : function(data, type, full, meta) {
							var editbut = '<button rpId="'
									+ data
									+ '" onclick="new tongfusoft.group().powerEdit(this)" type="button" data-toggle="modal" data-target="#delgroupClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

							return editbut;
						}

					} ]

			table.dataTable('powerTable', 'manager/power/query?groupId=' + $("#groupId").val(), columns, null);
			
			$('#powerTable').DataTable().on(
					'select',
					function(e, dt, type, indexes) {
						$("#rpId").val($('#powerTable').DataTable().rows(indexes).data()[0].rpId);
					});
		},

		//新增安全组
		addBtnGroup : function() {
			$("#addBtnGroup").click(function() {
				$("#groupModelTitle").text('新增');
				common.clean("groupForm");
				$('#groupModel').modal('show');
				$("#oper").val("add");

			})

		},
		//删除安全组
		groupDel :function(obj){
			var flag = false;
			common.post('manager/isGroupDoc', {
				"groupId" : $(obj).attr("groupId")
			}, function(data) {
				if (data.state == 'success' && data.data != 0) {
					flag = true;
				}else{
					flag = false;
				}
			});
			if(flag==true){
				toastr["error"]('该安全组正在使用中，无法删除!');
			}else{
				swal({
					title : "是否确认?",
					text : "",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "删除",
					cancelButtonText : "取消",
					closeOnConfirm : true
				}, function() {
					common.ajaxSubmitOnSuccess({
						"groupId" : $(obj).attr("groupId")
					}, 'manager/group/del', function(data) {

						if (data.state == 'success') {

							$('#groupTable').DataTable().ajax.reload();
							$('#powerTable').DataTable().ajax.reload();
						}else{
							toastr["error"]('删除失败！');
						}

					}, "del")
				});
			}
		},
		//编辑安全组
		groupEdit : function(obj) {

			common.post('manager/group/detail', {
				"groupId" : $(obj).attr("groupId")
			}, function(data) {
				$("#groupModelTitle").text('修改')
				$('#groupModel').modal('show');
				$("#oper").val("edit");
				if (data.state == 'success') {
					$('#groupName').val(data.data.groupName);
					$('#groupId').val(data.data.groupId);
				}else{
					toastr["error"]('未获取到该安全组信息!');
				}

			});

		},
		 //提交submit
		groupSubmit :function(){
			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("groupForm");
					var url = 'manager/group/add';

					if ($("#oper").val() == 'edit') {

						url = 'manager/group/edit';
						initData['groupId'] = $("#groupId").val();
					}
					common.ajaxSubmitOnSuccess(initData, url, function(data) {

						if (data.state == 'success') {
							$('#groupModel').modal('hide');
							$('#groupTable').DataTable().ajax.reload();
						} else if (data.code == 'repeat_error') {

							toastr["error"]('安全组名不能重复!');
						}else{
							toastr["error"]('保存失败!');
						}

					}, $("#oper").val())
				}
			});
			$("#groupForm").validate();

			$("#submitBtn").click(function() {

				$("#groupForm").submit();
			})
		},

		
		//获取权限
		isEnable : function() {
			var $readPower = $("#readPower");
			var $updatePower = $("#updatePower");
			var $deletePower=$("#deletePower");
			var $managerPower=$("#managerPower");
			var allCheck = document.getElementsByName("checkbox");
			var readPowerCheck = document.getElementById("readPowerCheck");
			var updatePowerCheck = document.getElementById("updatePowerCheck");
			var deletePowerCheck = document.getElementById("deletePowerCheck");
			var managerPowerCheck = document.getElementById("managerPowerCheck");
			
			$("#readPowerCheck").click(function() {
				if(readPowerCheck.checked == true) {
					$readPower.val("1")
				 } else {
					 for(var i = 0; i < allCheck.length; i++){
							allCheck[i].checked = false;
						}
					 managerPowerCheck.checked = false;
					 	$readPower.val("0")
						$updatePower.val("0")
						$deletePower.val("0")
						$managerPower.val("0")
				 }
			});
			
			$("#updatePowerCheck").click(function() {
				if(updatePowerCheck.checked == true) {
					readPowerCheck.checked = true
					$readPower.val("1")
					$updatePower.val("1")
				 } else {
					$updatePower.val("0")
					deletePowerCheck.checked = false;
					managerPowerCheck.checked = false;
					$managerPower.val("0")
					$deletePower.val("0")
				 }
			});
			
			$("#deletePowerCheck").click(function() {
				if(deletePowerCheck.checked == true) {
					readPowerCheck.checked = true
					updatePowerCheck.checked = true
					$readPower.val("1")
					$updatePower.val("1")
					$deletePower.val("1")
				 } else {
					$deletePower.val("0")
					managerPowerCheck.checked = false;
					$managerPower.val("0")
				 }
			});
			
			$("#managerPowerCheck").click(function() {
				if(managerPowerCheck.checked == true) {
					for(var i = 0; i < allCheck.length; i++){
						allCheck[i].checked = true;
					}
					$readPower.val("1")
					$updatePower.val("1")
					$deletePower.val("1")
					$managerPower.val("1")
				 } else {
					managerPowerCheck.checked = false;
					$readPower.val("1")
					$updatePower.val("1")
					$deletePower.val("1")
					$managerPower.val("0")
				 }
			});
		},
		//编辑权限
		powerEdit : function(obj){
			common.post('manager/powerRole/detail', {
				"rpId" : $(obj).attr("rpId"),
				"groupId":$("#groupId").val()
			}, function(data) {
				$("#powerModelTitle").text('编辑权限');
				$('#powerModel').modal('show');
				$("#power").val("editPower");
				if (data.state == 'success') {
					
					$('#readPower').val(data.data.readPower);
					$('#updatePower').val(data.data.updatePower);
					$('#deletePower').val(data.data.deletePower);
					$('#managerPower').val(data.data.managerPower);

					var read = document.getElementById('readPowerCheck');
					if(data.data.readPower == '1'){
						read.checked = true;
					}else{
						read.checked = false;
					}
					var update = document.getElementById('updatePowerCheck');
					if(data.data.updatePower == '1'){
						update.checked = true;				
					}else{
						update.checked = false;
					}
					var del = document.getElementById('deletePowerCheck');
					if(data.data.deletePower == '1'){
						del.checked = true;
					}else{
						del.checked = false;
					}
					var manager = document.getElementById('managerPowerCheck');
					if(data.data.managerPower == '1'){
						manager.checked = true;
					}else{
						manager.checked = false;
					}
				}else{
					toastr["error"]('未获取到权限信息');
				}

			});
		},
		//提交权限编辑
		powerSubmit :function(){
			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("powerForm");
					if ($("#power").val() == 'editPower') {
						url = 'manager/powerRole/edit';
						initData['rpId'] = $("#rpId").val();
					}
					
					common.ajaxSubmitOnSuccess(initData, url, function(data) {

						if (data.state == 'success') {
							toastr["success"]('添加成功!');
							$('#powerModel').modal('hide');
							$('#powerTable').DataTable().ajax.reload();
							
						} else {

							toastr["error"]('修改权限失败，未知错误!');
						}

					}, $("#power").val())
				}
			});
			$("#powerForm").validate();
			$("#updateBtn").click(function() {
				$("#powerForm").submit();
			})
		}
}

$(function() {

	var group = new tongfusoft.group();
	//安全组列表
	group.groupTable();
	//角色列表
	group.powerTable();
	//获取权限
	group.isEnable();
	//添加安全组
	group.addBtnGroup();
	//提交请求
	group.groupSubmit();
	//权限请求
	group.powerSubmit();
})